// 导入数据库操作模块
const db = require('../db/index')

//获取部门树形结构
exports.getdepartTree = (req, res) => {
    const root = 'select * from department '
    db.query(root, (err, results) => {
        if (err) return err
        // 调用函数转换查询结果为具有父子关系的数据结构
        const tree = buildHierarchy(results);
        // tree 即为树形结构，包含了所有部门的父子关系
        res.send(tree)
    })
}

exports.getDepartmentListLength = (req, res) => {
    const sql = 'select * from department  '
    db.query(sql, req.body.type, (err, result) => {
        if (err) return res.cc(err)
        res.send({
            length: result.length
        })
    })
}

//获取部门列表
exports.getdepartmentList = (req, res) => {
    console.log(req.body)
    const query = req.body
    const number = (query.pageCount - 1) * 10 //拿到offset的值
    const sql = `select dp.manager,dp.id,dp.department ,dp.departmentInclude,COUNT(us.name) as count from department  dp left join users us on dp.employeeId = us.id  GROUP BY dp.id,dp.department,dp.departmentInclude,dp.manager LIMIT 10 OFFSET ${number} `
    db.query(sql, (err, results) => {
        if (err) return err
        res.send(results)
    })
}

//根据部门名称查询部门内容
//获取部门列表
exports.getdepartmentListByName = (req, res) => {
    const query = req.body
    const number = (query.pageCount - 1) * 10 //拿到offset的值  
    const sql = `select dp.manager,dp.id,dp.department ,dp.departmentInclude,COUNT(us.name) as count from department  dp left join users us on dp.employeeId = us.id where dp.department = ? GROUP BY dp.id,dp.department,dp.departmentInclude,dp.manager LIMIT 10 OFFSET ${number} `
    db.query(sql, query.name, (err, results) => {
        if (err) return err
        res.send(results)
    })
}

//修改部门内容
exports.updatedepartment = (req, res) => {
    const query = req.body
    const sql = 'update department set ? where id = ?'

    db.query(sql, [{
        department: query.department,
        departmentInclude: query.departmentInclude,
        manager: query.manager
    }, query.departmentId], (err, results) => {
        if (err) return err
        res.send({
            status: 0,
            message: '修改成功'
        })
    })
}
//设置员工为部门主管
exports.updatedepartmentManager = (req, res) => {
    const query = req.body
    const sql = 'update department set ? where id = ?'
    db.query(sql, [{
        manager: query.manager,
    }, query.id], (err, results) => {
        if (err) return err
        res.send({
            status: 0,
            message: '设置成功!'
        })
    })
}

// 构建部门树形根节点
// 定义一个函数来转换查询结果为具有父子关系的数据结构
const buildHierarchy = (departments) => {
    const departmentMap = {}; // 用来存储部门名称与部门对象的映射关系

    // 遍历查询结果，创建部门对象并放入 departmentMap 中
    departments.forEach(department => {
        const { department: name, departmentInclude } = department;
        departmentMap[name] = {
            label: name,
            icon: 'el-icon-menu',
            children: []
        };
    });

    // 遍历查询结果，为每个部门对象设置父子关系
    departments.forEach(department => {
        const { department: name, departmentInclude } = department;
        const departmentNode = departmentMap[name];

        if (departmentInclude && departmentMap[departmentInclude]) {
            departmentMap[departmentInclude].children.push(departmentNode);
        }
    });
    // 找到根部门（没有父部门的部门），作为树的根节点
    const rootDepartments = departments.filter(department => !department.departmentInclude);

    return rootDepartments.map(department => departmentMap[department.department]);
};



